from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import IsAdminUser
from datetime import date
from django.utils import timezone
from datetime import timedelta
import pytz
from django.conf import settings
from rest_framework.generics import ListAPIView
from rest_framework.viewsets import ViewSet
from rest_framework.decorators import action

from users.models import User
from orders.models import *
from goods.models import GoodsVisitCount
from meiduo_admin.serializers.home_serializers import GoodsVisCountSerialzier

class HomeViewSet(ViewSet):

    @action(methods=['get'],detail=False)
    def total_count(self,request):
        user_qs = User.objects.all()
        count = user_qs.count()
        utc_time = timezone.now()
        local_time = utc_time.astimezone(tz=pytz.timezone(settings.TIME_ZONE))
        # local_time = utc_time.astimezone(tz=pytz.timezone(settings.TIME_ZONE))
        return Response({
            'count':count,
            'data':local_time.date()
        })

    @action(methods=['get'], detail=False)
    def day_increment(self,request):
        local_time = timezone.now().astimezone(tz=pytz.timezone(settings.TIME_ZONE))
        local_0_time = local_time.replace(hour=0,minute=0,second=0, microsecond=0)
        user_qs = User.objects.filter(date_joined__gte=local_0_time)
        count = user_qs.count()
        return Response({
            'count':count,
            'date':local_time.date()
        })

    @action(methods=['get'], detail=False)
    def day_active(self,request):
        local_0_time = timezone.now().astimezone(tz=pytz.timezone(settings.TIME_ZONE)).replace(hour=0,minute=0,second=0, microsecond=0)
        user_qs = User.objects.filter(last_login__gte=local_0_time)
        count = user_qs.count()
        return Response({
            "count":count,
            "date":local_0_time
        })

    @action(methods=['get'], detail=False)
    def day_orders(self,request):
        locals_0_time = timezone.now().astimezone(tz=pytz.timezone(settings.TIME_ZONE)).replace(hour=0,minute=0,second=0,microsecond=0)
        user_qs = User.objects.filter(orderinfo__create_time__gte=locals_0_time)
        count = user_qs.count()
        return Response({
            'count':count,
            'date':locals_0_time
        })

    @action(methods=['get'], detail=False)
    def month_increment(self,request):
        locals_0_time = timezone.now().astimezone(tz=pytz.timezone(settings.TIME_ZONE)).replace(hour=0,minute=0,second=0, microsecond=0)
        start_0_time = locals_0_time - timedelta(days=29)
        # start_0_time = cur_0_time - timedelta(days=29)
        user_list = []
        for index in range(30):
            calc_0_time = start_0_time + timedelta(days=index)
            next_0_time = calc_0_time + timedelta(days=1)
            user_qs = User.objects.filter(date_joined__gte=calc_0_time,date_joined__lt=next_0_time)
            count = user_qs.count()
            user_list.append({
                'count':count,
                'date':calc_0_time.date()
            })
        return Response(user_list)


class GoodsDayView(ListAPIView):
   queryset = GoodsVisitCount.objects.all()
   serializer_class = GoodsVisCountSerialzier

   def get_queryset(self):
        locals_0_time = timezone.now().astimezone(tz=pytz.timezone(settings.TIME_ZONE)).replace(hour=0,minute=0,second=0)
        return GoodsVisitCount.objects.filter(create_time__gte=locals_0_time)
